package com.fb;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.net.Uri;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.MimeTypeMap;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.dev.FileBrowserTab;
import com.dev.FileClipData;
import com.dev.FileProvider;
import com.dev.IconViewClose;
import com.dev.MainActivity;
import com.dev.QueryMakeShortcut;
import com.dev.QueryOpenFile;
import com.dev.QueryPaste;
import com.dev.ShowFileDetails;
import com.dev.TabBar;
import com.dev.TabData;
import com.fb.FileBrowserPage;
import com.fb.QueryRenameFile;
import com.log;
import java.io.File;
public class QueryActionForFile extends LinearLayout implements OnClickListener{
	final static String titleDefault="选项：";
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	LinearLayout titlebar;
	TextView title;
	IconViewClose ivc;
	public File destination;
	FileBrowserPage.Item itRename,itDelete,itDetails,itOpenInNewTab,itOpenas,itAdd;
	FileBrowserPage.Item it0,itCopy,itCut,itPaste,it1;
	LayoutParams itemlp;
	FileBrowserPage fbp;
	MainActivity ma;
	BatchRenameFile brf;
    QueryMakeShortcut qms;
	public QueryActionForFile(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setWillNotDraw(false);
		itemlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
		title=new TextView(ma);
		title.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
		title.setGravity(Gravity.CENTER_VERTICAL);
		title.setTextColor(0xff7f7f7f);
		title.setPadding(9,0,0,0);
		ivc=new IconViewClose(ma);
		ivc.setOnClickListener(this);
		titlebar=new LinearLayout(ma){
			@Override
			public void onDraw(Canvas c){
				super.onDraw(c);
				Paint p=new Paint();
				p.setColor(FileBrowserPage.borderColor);
				c.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
			}
		};
		titlebar.setWillNotDraw(false);
		titlebar.setOrientation(HORIZONTAL);
		titlebar.addView(title,new LayoutParams(0,-1,1));
		titlebar.addView(ivc);
		itDelete=new FileBrowserPage.Item(ma);
		itDelete.setText("删除");
		itDelete.setOnClickListener(this);
		itDetails=new FileBrowserPage.Item(ma);
		itDetails.setText("详情");
		itDetails.setOnClickListener(this);
		itCopy=new FileBrowserPage.Item(ma);
		itCopy.setText("复制");
		itCopy.setOnClickListener(this);
		itCut=new FileBrowserPage.Item(ma);
		itCut.setText("剪切");
		itCut.setOnClickListener(this);
		itPaste=new FileBrowserPage.Item(ma);
		itPaste.setText("粘贴");
		itPaste.setOnClickListener(this);
		itOpenInNewTab=new FileBrowserPage.Item(ma);
		itOpenInNewTab.setText("在新标签中打开目录");
		itOpenInNewTab.setOnClickListener(this);
		itAdd=new FileBrowserPage.Item(ma);
		itAdd.setText("在新标签中打开");
		itAdd.setOnClickListener(this);
		itOpenas=new FileBrowserPage.Item(ma);
		itOpenas.setText("打开为");
		itOpenas.setOnClickListener(this);
		itRename=new FileBrowserPage.Item(ma);
		itRename.setText("重命名");
		itRename.setOnClickListener(this);
		it0=new FileBrowserPage.Item(ma);
		it0.setText("其它应用打开");
		it0.setOnClickListener(this);
        it1=new FileBrowserPage.Item(ma);
        it1.setText("创建快捷方式");
        it1.setOnClickListener(this);
		addView(titlebar,new LayoutParams(-1,ma.dialogTitleBarHeight));
		addView(itRename,itemlp);
		addView(itDelete,itemlp);
		addView(itDetails,itemlp);
		addView(itCopy,itemlp);
		addView(itCut,itemlp);
		addView(itPaste,itemlp);
		addView(itOpenInNewTab,itemlp);
		addView(itAdd,itemlp);
		addView(itOpenas,itemlp);
		addView(it0,itemlp);
        addView(it1,itemlp);
	}
	int i,lim;
	public void show(FileBrowserPage fbp){
		this.fbp=fbp;
		if(ma.fileClipBoard.size()>0){
			itPaste.setVisibility(View.VISIBLE);
		}else{
			itPaste.setVisibility(View.GONE);
		}
		if(destination.isFile()){
			//itOpenInNewTab.setVisibility(View.GONE);
			itOpenas.setVisibility(View.VISIBLE);
			itAdd.setVisibility(View.VISIBLE);
			it0.setVisibility(View.VISIBLE);
			if(destination.length()>MainActivity.FILE_LENGTH_LIM){
				itAdd.setEnabled(false);
				itOpenas.setEnabled(false);
				itOpenInNewTab.setEnabled(false);
			}else{
				itAdd.setEnabled(true);
				itOpenas.setEnabled(true);
				itOpenInNewTab.setEnabled(true);
			}
		}else{
			itOpenInNewTab.setVisibility(View.VISIBLE);
			itOpenInNewTab.setEnabled(true);
			itOpenas.setVisibility(View.GONE);
			itAdd.setVisibility(View.GONE);
			it0.setVisibility(View.GONE);
		}
		File f=destination.getParentFile();
		if(f!=null){
			if(f.canWrite()){
				itRename.setEnabled(true);
			}else{
				itRename.setEnabled(false);
			}
		}
		if(fbp.selected==null){
			itOpenInNewTab.setEnabled(true);
			itOpenas.setEnabled(true);
			itAdd.setEnabled(true);
			it0.setEnabled(true);
			title.setText(titleDefault);
		}else{
			itOpenInNewTab.setEnabled(false);
			itOpenas.setEnabled(false);
			itAdd.setEnabled(false);
			it0.setEnabled(false);
			int count=0;
			for(int index=0;index<fbp.selected.length;index++){
				if(fbp.selected[index])count++;
			}
			title.setText("已选中"+count+"项");
		}
		wlp.width=ma.gui.getWidth()*4/5;
		wlp.height=-2;
		ma.wm.addView(this,wlp);
	}
	void dismiss(){
		ma.wm.removeView(this);
	}
	@Override
	public void onClick(View v){
		if(v==ivc){//onClick
			dismiss();
		}else if(v==itDelete){//onClick
			if(ma.confirmDeleteFile==null)ma.confirmDeleteFile=new ConfirmDeleteFile(ma);
			if(fbp.selected==null){
				dismiss();
				ma.confirmDeleteFile.destination=destination;
				ma.confirmDeleteFile.show(fbp);
			}else{
				dismiss();
				ma.confirmDeleteFile.destination=null;
				ma.confirmDeleteFile.show(fbp);
			}
		}else if(v==itOpenInNewTab){//onClick
			dismiss();
			if(destination.canRead()){
				if(destination.isDirectory()){
					FileBrowserTab fbt=new FileBrowserTab(ma);
					fbt.fbi=new TabData.FileBrowserInfo();
					fbt.fbi.home=destination;
                    fbt.fbi.currentDir=destination;
					fbt.setText(destination.getName());
					fbt.setOnClickListener(ma.gui.TabOnClick);
					ma.gui.tabs.addView(fbt,TabBar.tablp);
					ma.gui.TabOnClick.onClick(fbt);
					ma.gui.saveAllTab();
				}
			}else{
				Toast.makeText(ma,"目录不可读",0).show();
			}
		}else if(v==itRename){//onClick
			dismiss();
			if(fbp.selected==null){
				if(ma.qrf==null)ma.qrf=new QueryRenameFile(ma);
				ma.qrf.show(fbp,destination);
			}else{
				if(brf==null)brf=new BatchRenameFile(ma);
				brf.show(null);
			}
		}else if(v==itOpenas){//onClick
			dismiss();
			if(ma.queryOpenFile==null)ma.queryOpenFile=new QueryOpenFile(ma);
			ma.queryOpenFile.openInNewTab=false;
			ma.queryOpenFile.destination=destination;
			ma.queryOpenFile.show(fbp);
		}else if(v==itAdd){//onClick
			dismiss();
			if(ma.queryOpenFile==null)ma.queryOpenFile=new QueryOpenFile(ma);
			ma.queryOpenFile.openInNewTab=true;
			ma.queryOpenFile.destination=destination;
			ma.queryOpenFile.show(fbp);
		}else if(v==itDetails){//onClick
			if(ma.fileDetails==null)ma.fileDetails=new ShowFileDetails(ma);
			if(fbp.selected==null){
				ma.fileDetails.show(destination,fbp);
			}else{
				ma.fileDetails.show(null,fbp);
			}
		}else if(v==it0){//onClick
			String name=destination.getName();
			int ii=name.lastIndexOf('.');
			if(ii>=0){
				try{
					Uri uri=FileProvider.getUriForFile(ma,"com.remotefile.fileprovider",destination);
					Intent intent=new Intent(Intent.ACTION_VIEW);
					intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
					String ext=name.substring(ii+1);
					String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
					if(mime==null){
						intent.setDataAndType(uri,"application/octet-stream");
					}else{
						intent.setDataAndType(uri,mime);//log.alert(mime);
					}
					Intent chooser=Intent.createChooser(intent,"其它应用打开");
					ma.startActivity(chooser);
				}catch(Exception e){
					log.alert(e.toString());
				}
			}else{
				try{
					Uri uri=FileProvider.getUriForFile(ma,"com.remotefile.fileprovider",destination);
					Intent intent=new Intent(Intent.ACTION_VIEW,uri);
					intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
					intent.setDataAndType(uri,"application/octet-stream");
					Intent chooser=Intent.createChooser(intent,"其它应用打开");
					ma.startActivity(chooser);
				}catch(Exception e){
					log.alert(e.toString());
				}
			}
		}else if(v==it1){
            if(qms==null)qms=new QueryMakeShortcut(ma);
            qms.show(destination);
        }else if(v==itCut){//onClick
			if(fbp.selected==null){
				if(destination.canWrite()){
					for(int ii=0,lim=ma.fileClipBoard.size();true;ii++){
						if(ii<lim){
							File f=ma.fileClipBoard.get(ii).f;
							if(f.equals(destination)){
								Toast.makeText(ma,"已存在此剪切，无须此操作",0).show();
								break;
							}
						}else{
							dismiss();
							FileClipData fcd=new FileClipData();
							fcd.f=destination;
							fcd.move=true;
							if(ma.fileClipBoard.add(fcd)){
								Toast.makeText(ma,"已添至剪贴板",0).show();
								fbp.ivPaste.setVisibility(View.VISIBLE);
							}
							break;
						}
					}
				}else{
					Toast.makeText(ma,"不可写",0).show();
				}
			}else{
				int size=ma.fileClipBoard.size();
				int count=0;
				for(int index=0;index<fbp.selected.length;index++){
					if(fbp.selected[index]){
						File f1=fbp.willshow[index];
						for(int i2=0;true;i2++){
							if(i2<size){
								File f2=ma.fileClipBoard.get(i2).f;
								if(f2.equals(f1)){
									break;
								}
							}else{
								FileClipData fcd=new FileClipData();
								fcd.move=true;
								fcd.f=f1;
								count++;
								ma.fileClipBoard.add(fcd);
								break;
							}
						}
					}
				}
				if(ma.fileClipBoard.size()>0){
					Toast.makeText(ma,count+"项已剪切",0).show();
					fbp.ivPaste.setVisibility(View.VISIBLE);
				}
				fbp.selected=null;
				fbp.box.setVisibility(GONE);
				fbp.dlv.invalidate();
				dismiss();
			}
		}else if(v==itPaste){//onClick
			if(fbp.selected==null){
				if(destination.isDirectory()){
					if(destination.canWrite()){
						if(ma.queryPaste==null)ma.queryPaste=new QueryPaste(ma);
						ma.queryPaste.startPaste(fbp,destination);
						dismiss();
					}else{
						Toast.makeText(ma,"不可写",0).show();
					}
				}
			}else{
				int size=ma.fileClipBoard.size();
				int count=0;
				for(int i1=0;i1<fbp.selected.length;i1++){
					if(fbp.selected[i1]){
						File f1=fbp.willshow[i1];
						for(int i2=0;true;i2++){
							if(i2<size){
								File f2=ma.fileClipBoard.get(i2).f;
								if(f2.equals(f1)){
									break;
								}
							}else{
								FileClipData fcd=new FileClipData();
								fcd.move=false;
								fcd.f=f1;
								count++;
								ma.fileClipBoard.add(fcd);
								break;
							}
						}
					}
				}
				if(ma.fileClipBoard.size()>0){
					Toast.makeText(ma,count+"项已复制",0).show();
					fbp.ivPaste.setVisibility(View.VISIBLE);
				}
				fbp.selected=null;
				fbp.box.setVisibility(GONE);
				fbp.dlv.invalidate();
				dismiss();
			}
		}else if(v==itCopy){//onClick()
			if(fbp.selected==null){
				if(destination.canRead()){
					for(int ii=0,lim=ma.fileClipBoard.size();true;ii++){
						if(ii<lim){
							File f=ma.fileClipBoard.get(ii).f;
							if(f.equals(destination)){
								Toast.makeText(ma,"已存在此剪切，无须此操作",0).show();
								break;
							}
						}else{
							dismiss();
							FileClipData fcd=new FileClipData();
							fcd.f=destination;
							fcd.move=false;
							if(ma.fileClipBoard.add(fcd)){
								Toast.makeText(ma,"已复制",0).show();
								//try{
								fbp.ivPaste.setVisibility(VISIBLE);
								//}catch(Exception e){log.alert(e.toString());}
							}
							break;
						}
					}
				}else{
					Toast.makeText(ma,"不可读",0).show();
				}
			}else{
				int lim=ma.fileClipBoard.size();
				int count=0;
				for(int i1=0;i1<fbp.selected.length;i1++){
					if(fbp.selected[i1]){
						File f1=fbp.willshow[i1];
						for(int i2=0;true;i2++){
							if(i2<lim){
								File f2=ma.fileClipBoard.get(i2).f;
								if(f2.equals(f1)){
									break;
								}
							}else{
								FileClipData fcd=new FileClipData();
								fcd.move=false;
								fcd.f=f1;
								count++;
								ma.fileClipBoard.add(fcd);
								break;
							}
						}
					}
				}
				if(ma.fileClipBoard.size()>0){
					Toast.makeText(ma,count+"项已复制",0).show();
					fbp.ivPaste.setVisibility(View.VISIBLE);
				}
				fbp.selected=null;
				fbp.box.setVisibility(GONE);
				fbp.dlv.invalidate();
				dismiss();
			}
			//}catch(Exception e){log.alert(e.toString());}
		}
	}
	@Override
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		Paint p=new Paint();
		p.setColor(FileBrowserPage.borderColor);
		canvas.drawColor(0xffffffff);
		canvas.drawRect(0,0,FileBrowserPage.borderWidth,getHeight(),p);
		canvas.drawRect(0,0,getWidth(),FileBrowserPage.borderWidth,p);
		canvas.drawRect(getWidth()-FileBrowserPage.borderWidth,0,getWidth(),getHeight(),p);
		canvas.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
	}
	@Override
	public boolean onTouchEvent(MotionEvent event){
		int rx,ry;
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:
				rx=(int)event.getRawX();
				ry=(int)event.getRawY();
				int[] loc=new int[2];
				getLocationOnScreen(loc);
				if(rx<loc[0]||ry<loc[1]||rx>=(loc[0]+getWidth())||ry>=(loc[1]+getHeight())){
					dismiss();
				}
				break;
			case MotionEvent.ACTION_OUTSIDE:
				dismiss();
		}
		return super.onTouchEvent(event);
	}
}
